package com.dbq.fast.business.setting;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.widget.TextView;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.h5.H5ContainerActivity;
import com.dbq.fast.business.login.bean.CustomProfile;
import com.dbq.fast.business.login.business.LoginBiz;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.common.manager.DatabaseManager;

import static com.dbq.fast.business.h5.H5ContainerActivity.WHICH_IS_PRIVACY;

public class SettingActivity extends BaseActivity {

    private TextView whiteListTV;

    LoginBiz loginBiz;
    CustomProfile profile;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        loginBiz = new LoginBiz(this);
        profile = DatabaseManager.getInstance().queryProfile();
        whiteListTV = findViewById(R.id.white_list);
        initListener();
    }

    private void initListener() {
        findViewById(R.id.back).setOnClickListener(view -> {
            onBackPressed();
        });
        findViewById(R.id.privacy).setOnClickListener(view -> {
            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_CLICK_TERMS);
            Intent intent = new Intent(this, H5ContainerActivity.class);
            intent.putExtra("which", WHICH_IS_PRIVACY);
            startActivity(intent);
        });
        findViewById(R.id.about_us).setOnClickListener(view -> {
            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_CLICK_ABOUT_US);
            startActivity(new Intent(this, AboutActivity.class));
        });
        whiteListTV.setOnClickListener(view -> add2Protect());

        findViewById(R.id.update_version).setOnClickListener(v -> {
            updateLogic.checkVersion();
            YibaAnalysis.getInstance().event(SettingActivity.this, EventConstant.HAND_AUTO_UPDATE);
        });

    }



    /**
     * 加入受保护应用跳转代码
     * 针对华为手机较高操作系统优化处理
     */
    private void add2Protect() {
        if ("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                startActivity(intent);
            } catch (Exception e) {
            }
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.white_list_title)
                    .setMessage(Html.fromHtml(getString(R.string.white_list_content)))
                    .setPositiveButton(getString(R.string.known), (dialogInterface, i) -> {
                    }).create().show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
